package com.mju.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mju.controller.common.BaseController;
import com.mju.controller.common.JsonResult;
import com.mju.pojo.Product;
import com.mju.service.IProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/products")
@Api(tags = "商品信息服务")
public class ProductController extends BaseController {
    @Autowired
    private IProductService productService;

    @ApiOperation("获取商品热门的前四条数据")
    @GetMapping("/hot_list")
    public JsonResult<List<Product>> getHotList() {
        List<Product> data = productService.selectHotList();
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("获取商品最新的前四条数据")
    @GetMapping("/new_list")
    public JsonResult<List<Product>> getNewProductList() {
        List<Product> data = productService.selectNewProductList();
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("商品详情页")
    @GetMapping("/{id}/details")
    public JsonResult<Product> getById(@PathVariable("id") Integer id) {
        Product data = productService.selectById(id);
        return new JsonResult<>(OK, data);
    }

    // 处理根据产品关键字进行模糊查询的请求
    @ApiOperation("处理根据产品关键字进行模糊查询的请求")
    @GetMapping("/{currentPage}/{pageSize}/{title}")
    public JsonResult<IPage<Product>> queryByTitle(@PathVariable("currentPage") Integer currentPage,
                                                     @PathVariable("pageSize") Integer pageSize,
                                                     @PathVariable("title") String title){
        IPage<Product> data = productService.queryProductByTitle(currentPage, pageSize, title);
        return new JsonResult<>(OK, data);
    }

    // 根据商品类型查询数据
    @ApiOperation("根据商品类型查询数据")
    @GetMapping("/{currentPage}/{pageSize}")
    public JsonResult<IPage<Product>> queryByType(@PathVariable("currentPage") Integer currentPage,
                                                   @PathVariable("pageSize") Integer pageSize,
                                                   @RequestParam String type){
        IPage<Product> data = productService.queryProductByType(currentPage, pageSize, type);
        return new JsonResult<>(OK, data);
    }

    // 无条件查询所有商品
    @ApiOperation("无条件查询所有商品")
    @GetMapping("/queryAll/{currentPage}/{pageSize}")
    public JsonResult<IPage<Product>> queryAllProduct(@PathVariable("currentPage") Integer currentPage,
                                               @PathVariable("pageSize") Integer pageSize) {
        IPage<Product> data = productService.queryAllProduct(currentPage, pageSize);
        return new JsonResult<>(OK, data);
    }

    // 添加商品
    @ApiOperation("添加商品")
    @PostMapping("/createProduct")
    public JsonResult<Void> createProduct(@RequestBody Product product, HttpServletRequest request) {
        // 获取uname
        String token = request.getHeader("Authorization");
        Map<String, String> map = parseTokenGetUidAndUname(token);
        String uname = map.get("uname");
        Integer result = productService.createProduct(product, uname);
        return new JsonResult<>(OK);
    }
    // 删除商品
    @ApiOperation("删除商品")
    @DeleteMapping("/deleteProduct/{pid}")
    public JsonResult<Integer> deleteProduct(@PathVariable("pid")Integer pid) {
        Integer data = productService.deleteProduct(pid);
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("修改商品")
    @PutMapping("/updateProduct/{pid}")
    public JsonResult<Integer> updateProduct(@PathVariable Integer pid, @RequestBody Product product) {
        Integer data = productService.updateProduct(pid, product);
        return new JsonResult<>(OK, data);
    }

    @ApiOperation("获取商品编号通过标题")
    @GetMapping("/getPidByTitle/{title}")
    public JsonResult<Integer> getPidByTitle(@PathVariable("title") String title) {
        Integer pid = productService.getPidByTitle(title);
        return new JsonResult<>(OK, pid);
    }
}
